package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 积分等级管理
 */
@Api(tags = "积分等级管理")
@RestController
@RequestMapping(value = "/admin/core/integralGrade")
@RequiredArgsConstructor
public class AdminIntegralGradeController {

    // 积分等级表 服务类
    private final IntegralGradeService integralGradeService;

    /**
     * 积分等级列表
     */
    @ApiOperation("积分等级列表")
    @GetMapping(value = "list")
    public R list() {
        List<IntegralGrade> list = integralGradeService.list();

        return R.ok().data("list", list);
    }

    /**
     * 根据id删除积分等级
     */
    @ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")
    @DeleteMapping(value = "/remove/{id}")
    public R removeById(@ApiParam(value = "数据id", required = true, example = "100") @PathVariable Long id) {
        boolean result = integralGradeService.removeById(id);

        return result ? R.ok().message("删除成功") : R.error().message("删除失败");
    }

    /**
     * 新增积分等级
     */
    @ApiOperation("新增积分等级")
    @PostMapping(value = "/save")
    public R save(@ApiParam(value = "积分等级对象", required = true) @RequestBody IntegralGrade integralGrade) {

        // BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = integralGradeService.save(integralGrade);

        return result ? R.ok().message("保存成功") : R.error().message("保存失败");
    }

    /**
     * 更新积分等级
     */
    @ApiOperation("更新积分等级")
    @PutMapping(value = "/update")
    public R updateById(@ApiParam(value = "积分等级对象", required = true) @RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.updateById(integralGrade);

        return result ? R.ok().message("修改成功") : R.error().message("修改失败");
    }

    /**
     * 根据id查询积分等级
     */
    @ApiOperation(value = "根据id查询积分等级")
    @GetMapping(value = "get/{id}")
    public R getById(@ApiParam(value = "数据id", required = true, example = "100") @PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);

        return R.ok().data("record", integralGrade);
    }

}
